﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Carer.Data;
using Carer.Data.Models;
using System.Runtime.InteropServices;
using System.Web.Mvc;
using Carer.Crm.Authorize;

namespace Carer.Crm.Controllers
{
    [SessionAuthorize]
    public class ConfigController : BaseController
    {
        public ConfigController()
        {

        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteUnit(int[] ids)
        {
            if ((ids == null) || (ids.Length == 0))
            {
                return base.Json(new SubmitResult(false, "请选择至少一条记录"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.DeleteUnit(ids))
            {
                base.Session["all"] = DataService.GetAllUnits();
                return base.Json(new SubmitResult(true, "单位记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "单位记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteUser(int id)
        {
            if (DataService.DeleteUser(id))
            {
                return base.Json(new SubmitResult(true, "用户记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "用户记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult DeleteUsers(int[] ids)
        {
            if ((ids == null) || (ids.Length == 0))
            {
                return base.Json(new SubmitResult(false, "请选择至少一条用户记录"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.DeleteUser(ids))
            {
                return base.Json(new SubmitResult(true, "用户记录删除成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "用户记录删除失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult EditUnit(Unit h)
        {
            if (string.IsNullOrEmpty(h.Name))
            {
                SubmitResult data = new SubmitResult(false, "单位名称不能为空");
                return base.Json(data, System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (!h.Order.HasValue)
            {
                SubmitResult data = new SubmitResult(false, "排序不能为空");
                return base.Json(data, System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.EditUnit(h))
            {
                base.Session["all"] = DataService.GetAllUnits();
                return base.Json(new SubmitResult(true, "单位数据保存成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "单位数据保存失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult EditUser(User u)
        {
            if ((string.IsNullOrEmpty(u.Name) || string.IsNullOrEmpty(u.Password)) || string.IsNullOrEmpty(u.PassAgain))
            {
                return base.Json(new SubmitResult(false, "用户名和密码不能为空"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (u.Password != u.PassAgain)
            {
                return base.Json(new SubmitResult(false, "两次密码输入不相符"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (!u.Role.HasValue)
            {
                return base.Json(new SubmitResult(false, "必须选择一个角色"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (!((u.Role <= 0) || u.UnitId.HasValue))
            {
                return base.Json(new SubmitResult(false, "非系统管理员必须设定所属单位"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            if (DataService.EditUser(u))
            {
                return base.Json(new SubmitResult(true, "用户数据保存成功"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
            }
            return base.Json(new SubmitResult(false, "用户数据保存失败"), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult GetUnit(int id)
        {

            return base.Json(DataService.GetUnit(id), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult GetUser(int id)
        {
            return base.Json(DataService.GetUser(id), System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryUnit(UnitQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["UnitQuery"] as UnitQuery;
                if (q == null)
                {
                    base.Session["UnitQuery"] = q = new UnitQuery();
                }
            }
            else
            {
                base.Session["UnitQuery"] = q;
            }
            QueryResult<Unit> units = DataService.GetUnits(q);
            return base.Json(units, System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult QueryUser(UserQuery q, bool keep = false)
        {
            if (keep)
            {
                q = base.Session["UserQuery"] as UserQuery;
                if (q == null)
                {
                    base.Session["UserQuery"] = q = new UserQuery();
                }
            }
            else
            {
                base.Session["UserQuery"] = q;
            }
            QueryResult<User> users = DataService.GetUsers(q);
            return base.Json(users, System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult UnitPage(int page)
        {
            UnitQuery q = base.Session["UnitQuery"] as UnitQuery;
            if (q == null)
            {
                base.Session["UnitQuery"] = q = new UnitQuery();
            }
            q.PageNumber = page;
            QueryResult<Unit> units = DataService.GetUnits(q);
            return base.Json(units, System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        public System.Web.Mvc.ActionResult UnitView()
        {
            return base.ViewModel(new List<string>() { "系统管理", "单位管理" });
        }

        [System.Web.Mvc.OutputCache(Duration = 0)]
        public System.Web.Mvc.JsonResult UserPage(int page)
        {
            UserQuery q = base.Session["UserQuery"] as UserQuery;
            if (q == null)
            {
                base.Session["UserQuery"] = q = new UserQuery();
            }
            q.PageNumber = page;
            QueryResult<User> users = DataService.GetUsers(q);
            return base.Json(users, System.Web.Mvc.JsonRequestBehavior.AllowGet);
        }

        public System.Web.Mvc.ActionResult UserView()
        {
            return base.ViewModel(new List<string>() { "系统管理", "用户管理" });
        }
    }
}
